package com.example.sakurasay.quicknotes

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.outlined.Description
import androidx.compose.material.icons.outlined.EditNote
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.shape.RoundedCornerShape


@OptIn(ExperimentalMaterial3Api::class)
data class Note(val id: Int, val title: String, val content: String, val timestamp: String)

val sampleNotes = listOf(
    Note(1, "购物清单", "牛奶、面包、鸡蛋", "2023-10-27 10:00"),
    Note(2, "会议纪要", "讨论了Q4季度的新功能计划。", "2023-10-26 15:30"),
    Note(3, "灵感", "一个关于时间旅行的短篇小说点子。", "2023-10-25 20:15")
)

@Composable
fun QuickNotesScreen() {
    var showAddSheet by remember { mutableStateOf(false) }
    val notes = remember { mutableStateListOf(*sampleNotes.toTypedArray()) }

    Box(modifier = Modifier.fillMaxSize()) {
        LazyColumn(
            contentPadding = PaddingValues(horizontal = 16.dp, vertical = 12.dp),
            verticalArrangement = Arrangement.spacedBy(12.dp),
        ) {
            items(notes) { note ->
                NoteListItem(note = note)
            }
        }
        FloatingActionButton(
            onClick = { showAddSheet = true },
            containerColor = MaterialTheme.colorScheme.secondary,
            contentColor = MaterialTheme.colorScheme.onSecondary,
            modifier = Modifier
                .align(Alignment.BottomEnd)
                .padding(16.dp),
            shape = RoundedCornerShape(16.dp)
        ) {
            Icon(Icons.Default.Add, contentDescription = "Add Note")
        }

        if (showAddSheet) {
            AddNoteBottomSheet(
                onDismiss = { showAddSheet = false },
                onSave = { newNote ->
                    notes.add(newNote)
                    showAddSheet = false
                }
            )
        }
    }
}

@Composable
fun NoteListItem(note: Note) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .height(80.dp), // Set a fixed height for the card
        shape = RoundedCornerShape(20.dp), // Increased corner radius
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surfaceVariant,
        ),
        elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
    ) {
        Row(
            modifier = Modifier
                .padding(horizontal = 24.dp) // Increased padding
                .fillMaxHeight(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                imageVector = Icons.Outlined.Description,
                contentDescription = "Note Icon",
                tint = MaterialTheme.colorScheme.secondary,
                modifier = Modifier.size(48.dp) // Increased icon size
            )
            // The rest of the content is removed to match the simple design
        }
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddNoteBottomSheet(
    onDismiss: () -> Unit,
    onSave: (Note) -> Unit
) {
    val sheetState = rememberModalBottomSheetState()
    var title by remember { mutableStateOf("") }
    var content by remember { mutableStateOf("") }

    ModalBottomSheet(
        onDismissRequest = onDismiss,
        sheetState = sheetState
    ) {
        Column(modifier = Modifier.padding(16.dp).navigationBarsPadding()) {
            Text(
                text = "添加新笔记",
                style = MaterialTheme.typography.headlineSmall,
                modifier = Modifier.padding(bottom = 16.dp)
            )
            TextField(
                value = title,
                onValueChange = { title = it },
                label = { Text("标题") },
                modifier = Modifier.fillMaxWidth()
            )
            Spacer(modifier = Modifier.height(8.dp))
            TextField(
                value = content,
                onValueChange = { content = it },
                label = { Text("内容") },
                modifier = Modifier.fillMaxWidth()
            )
            Spacer(modifier = Modifier.height(16.dp))
            Button(
                onClick = {
                    val newNote = Note(
                        id = (0..1000).random(), // Replace with proper ID generation
                        title = title,
                        content = content,
                        timestamp = "2023-10-28" // Replace with actual date
                    )
                    onSave(newNote)
                },
                modifier = Modifier.fillMaxWidth()
            ) {
                Text("保存")
            }
        }
    }
}